package com.sample.user.web;

import java.util.concurrent.CompletableFuture;

import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Method;

import com.sample.user.entity.User;
import com.sample.user.service.UserService;
import com.sample.user.service.UserServiceAsync;
import com.swak.annotation.Get;
import com.swak.annotation.RestApi;
import com.swak.entity.Result;

/**
 * 用户API
 * 
 * @author lifeng
 * @date 2020年7月30日 下午9:06:25
 */
@RestApi(path = "/user")
public class UserApi {

	@DubboReference(sticky = true, group = "sj", stub = "true", mock = "true", actives = 5, methods = {
			@Method(name = "get", actives = 10) })
	private UserService userService;

	@DubboReference(sticky = true, group = "sj", stub = "true")
	private UserServiceAsync userServiceAsync;

	@Get("/get")
	public CompletableFuture<Result> get() {
		return userServiceAsync.get(1L).thenApply(user -> {
			return Result.success(user);
		});
	}

	/**
	 * 会阻塞当前的线程
	 * 
	 * @return
	 */
	@Get("/get/sync")
	public Result getSync() {
		User user = userService.get(2L);
		return Result.success(user);
	}
}